//
//  NSString+NSStringForJava.m
//  homeland
//
//  Created by gzbugu on 14-7-31.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  扩展NSString 部分方法为java 风格
//

#import "NSString+NSStringForJava.h"

@implementation NSString (NSStringForJava)

//对比两个字符串内容是否一致
- (BOOL) equals:(NSString*) string
{
    return [self isEqualToString:string];
}

//判断字符串是否以指定的前缀开头
- (BOOL) startsWith:(NSString*)prefix
{
    return [self hasPrefix:prefix];
}

//判断字符串是否以指定的后缀结束
- (BOOL) endsWith:(NSString*)suffix
{
    return [self hasSuffix:suffix];
}

//转换成大写
- (NSString *) toLowerCase
{
    return [self lowercaseString];
}

//转换成小写
- (NSString *) toUpperCase
{
    return [self uppercaseString];
}

//截取字符串前后空格
- (NSString *) trim
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

//用指定分隔符将字符串分割成数组
- (NSArray *) split:(NSString*) separator
{
    return [self componentsSeparatedByString:separator];
}

//用指定字符串替换原字符串
- (NSString *) replaceAll:(NSString*)oldStr with:(NSString*)newStr
{
    return [self stringByReplacingOccurrencesOfString:oldStr withString:newStr];
}

//从指定的开始位置和结束位置开始截取字符串
- (NSString *) substringFromIndex:(int)begin toIndex:(int)end
{
    if (end <= begin) {
        return @"";
    }
    NSRange range = NSMakeRange(begin, end - begin);
    return [self substringWithRange:range];
}

//判断是否为整形对象：
- (BOOL)isPureInteger:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    NSInteger val;
    return[scan scanInteger:&val] && [scan isAtEnd];
}

//判断是否为整形：
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

//判断是否为浮点形：
- (BOOL)isPureFloat:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
}

//判断是否为双精度形：
- (BOOL)isPureDouble:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    double val;
    return[scan scanDouble:&val] && [scan isAtEnd];
}

//判断是否为长整形：
- (BOOL)isPureLong:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    long long val;
    return[scan scanLongLong:&val] && [scan isAtEnd];
}

//判断是否为高精度形：
- (BOOL)isPureDecimal:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    NSDecimal val;
    return[scan scanDecimal:&val] && [scan isAtEnd];
}

//转换数值型字符串为纯字符串
- (NSString *)toPureString:(NSString*)string{
    NSString *newString;
    //double字符串
    if ([self isPureDouble:string]) {
        double doubleString = [string doubleValue];
        newString = [NSString stringWithFormat:@"%f",doubleString];
    }else
    //int字符串
    if ([self isPureInt:string]) {
        int intString = [string intValue];
        newString = [NSString stringWithFormat:@"%d",intString];
    }else
    //Integer字符串
    if ([self isPureInteger:string]) {
        NSInteger integerString = [string integerValue];
        newString = [NSString stringWithFormat:@"%d",integerString];
    }else
    //float字符串
    if ([self isPureFloat:string]) {
        float floatString = [string floatValue];
        newString = [NSString stringWithFormat:@"%f",floatString];
    }else
    //long字符串
    if ([self isPureLong:string]) {
        long long longString = [string longLongValue];
        newString = [NSString stringWithFormat:@"%lld",longString];
    }else
    //decimal字符串
    if ([self isPureDecimal:string]) {
        double decimalString = [string doubleValue];
        newString = [NSString stringWithFormat:@"%f",decimalString];
    }else{
        newString = string;
    }
    return newString;
}

//高精度计算
-(NSString*)decimalNumberMutiplyWithString:(NSString *)multiplierValue multiplicandValue:(NSString *)multiplicandValue{
    
    NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:multiplierValue];
    
    NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:multiplicandValue];
    
    NSDecimalNumber *product = [multiplicandNumber decimalNumberByMultiplyingBy:multiplierNumber];
    
    return [product stringValue];
}
@end